// build ban strength with two new measures

// FIRST NEW MEASURE: calculate ban intensity only for meaningful scenarios
********************************************************************************
* SETUP SCENARIOS
********************************************************************************
clear
tempfile time
clear 
set obs 22
gen year = 2008+_n
save `time'
	

* Inputting different scenarios
clear
* We have 74 banks, when everything is turned on
** NOTE made changes for isThermal
//																				1 						2 				3 		   4  		 5			6				7 			8 		 9         10         11     12               13                    14                  15                      
input scenario isProjFin isCorpFin str50 ContinentParent str50 CountryParent CoalFracRevParent CoalSharePowerParent isMiningCo isPowerCo isMiningProj isPowerProj isNewCustomer isLignite isThermal isExpansion isNew isLowCarbonProj isMountaintopProj isMountaintopComp hasDecarbonStrat  str50 CountryGroup
//										      X X    <-- minin project and power project

//Atrribute Number (see ABOVE)  1   2   3 4 5 6 7 8 9 10 11 12 13 14 15
1 1 0 "North America"  "USA"    1   1   0 1 0 1 1 0 1 0  1  0  0  0  0  "OECD" // Power Project, isNew, isNewCustomer, Project finance
2 1 0 "North America"  "USA"    1   1   0 1 0 1 0 0 1 0  1  0  0  0  0  "OECD" // Power Project, isNew, isOldCustomer, Project finance
3 1 0 "North America"  "USA"    1   1   0 1 0 1 1 0 1 1  0  0  0  0  0  "OECD" // Power Project, isExpansion, isNewCustomer, Project finance
4 1 0 "North America"  "USA"    1   1   0 1 0 1 0 0 1 1  0  0  0  0  0  "OECD" // Power Project, isExpansion, isOldCustomer, Project finance
5 0 1 "North America"  "USA"    1   1   0 1 0 1 1 0 1 0  0  0  0  0  0  "OECD" // Power Project, isNewCustomer, Corp finance
6 0 1 "North America"  "USA"    0.2 0.2 0 1 0 1 1 0 1 0  0  0  0  0  0  "OECD" // Power Project, isNewCustomer, Corp finance, revenue=0.2
7 0 1 "North America"  "USA"    0.2 0.2 0 1 0 1 0 0 1 0  0  0  0  0  0  "OECD" // Power Project, isOldCustomer, Corp finance, revenue=0.2
8 1 0 "North America"  "USA"    1   1   1 0 1 0 1 0 1 0  1  0  0  0  0  "OECD" // Mining Project, isNew, isNewCustomer, Project finance
9 1 0 "North America"  "USA"    1   1   1 0 1 0 1 0 0 1  0  1  0  0  0   "OECD" // Mining Project, isNew, isOldCustomer, Project finance
10 1 0 "North America" "USA"    1   1   1 0 1 0 1 0 1 1  0  0  0  0  0  "OECD" // Mining Project, isExpansion, isNewCustomer, Project finance
11 1 0 "North America" "USA"    1   1   1 0 1 0 0 0 1 1  0  0  0  0  0  "OECD" // Mining Project, isExpansion, isOldCustomer, Project finance
12 0 1 "North America" "USA"    1   1   1 0 1 0 1 0 1 0  0  0  0  0  0  "OECD" // Mining Project, isNewCustomer, Corp finance
13 0 1 "North America" "USA"    0.2 0.2 1 0 1 0 1 0 1 0  0  0  0  0  0  "OECD" // Mining Project, isNewCustomer, Corp finance, revenue=0.2
14 0 1 "North America" "USA"    0.2 0.2 1 0 1 0 0 0 1 0  0  0  0  0  0  "OECD" // Mining Project, isOldCustomer, Corp finance, revenue=0.2

//extra secenarios             	1   2   3 4 5 6 7 8 9 10 11 12 13 14 15
101 1 0 "North America" "USA" 	0   0   0 1 0 1 0 0 1 0  1  0  0  0  0    "OECD" // Power Project Finance, New
102 1 0 "North America" "USA" 	0   0   0 1 0 1 0 0 1 1  0  0  0  0  0    "OECD" // Power Project Finance, Expansion
103 0 1 "North America" "USA" 	0   0   0 1 0 1 0 0 1 0  0  0  0  0  0    "OECD" // Power Company Corporate Finance (also screen on isPowerProj)
104 1 0 "North America" "USA" 	0   0   1 0 1 0 0 0 0 0  1  0  0  0  0    "OECD" // Mining Project Finance, New
105 1 0 "North America" "USA" 	0   0   1 0 1 0 0 0 0 1  0  0  0  0  0    "OECD" // Mining Project Finance, Expansion
106 0 1 "North America" "USA" 	0   0   1 0 1 0 0 0 0 0  0  0  0  0  0    "OECD" // Mining Company Corporate Finance (also screen on isMiningProj)


end
cross using `time'

gen isHighCarbonProj = 1-isLowCarbonProj
gen hasNoDecarbonStrat  = 1-hasDecarbonStrat

//drop impossible scenarios. None of these are generated above anyway, so below doesn't do anything.
drop if isMiningCo == 0 & isMountaintopComp == 1
drop if isMiningProj == 0 & isMountaintopProj == 1
drop if isMiningProj == 1 & isPowerProj == 1
drop if isNew == 1 & isExpansion == 1
drop if isCorpFin == 1 & isProjFin == 1
drop if isPowerProj == 1 & isThermal == 0
drop if isPowerCo == 1 & isThermal == 0

save "../Intermediate/policy_coding_setup_blank", replace


********************************************************************************
* STANDARD POLICY CODING
********************************************************************************
use "../Intermediate/policy_coding_setup_blank", clear
include "../Data_Cleaning/Policy_Coding_Function.do"


//save bank x year x scenario ban dataset 
preserve 
keep if scenario > 100
greshape long ban_, i(scenario year) j(BankID)
order scenario year BankID
sort BankID year scenario
replace ban_ = 0 if missing(ban_)
gen scenario_str = "Power ProjFin New" if scenario == 101
replace scenario_str = "Power ProjFin Expansion" if scenario == 102
replace scenario_str = "Power CorpFin" if scenario == 103
replace scenario_str = "Mining ProjFin New" if scenario == 104
replace scenario_str = "Mining ProjFin Expansion" if scenario == 105
replace scenario_str = "Mining CorpFin" if scenario == 106
keep scenario scenario_str BankID ban_ year 
order scenario scenario_str year BankID ban_ 
rename ban_ ban 
save ../Intermediate/broad_scenarios_bank_year_ban, replace
restore
//get rid of extra secenarios used for graph
drop if scenario > 100

gen power_scenario = inrange(scenario,1,7)
gen mining_scenario = inrange(scenario,8,14)


*scenarios applying to firms not expanding
gen nongrowth_scenario= inrange(scenario,5,7) | inrange(scenario,12,14)

gen lowgrowth_scenario= inrange(scenario,3,7) | inrange(scenario,10,14)


*scenarios applying to firms with low coal share
gen lowshare_scenario= inrange(scenario,6,7) | inrange(scenario,13,14)

gen highshare_scenario= 1-lowshare_scenario

**that may not be enough scenarios conditional on that variable, for instance don't account for expansion.


count if year == 2009
local scenario_count = r(N)
count if year == 2009 & power_scenario
local scenario_count_power = r(N)
count if year == 2009 & mining_scenario
local scenario_count_mining = r(N)
count if year == 2009 & nongrowth_scenario
local scenario_count_nongrowth = r(N)
count if year == 2009 & lowgrowth_scenario
local scenario_count_lowgrowth = r(N)
count if year == 2009 & lowshare_scenario
local scenario_count_lowshare = r(N)
count if year == 2009 & highshare_scenario
local scenario_count_highshare = r(N)

foreach var of var ban_* {
	local bank = substr("`var'",5,.)
	gen ban_power_`bank' = `var'*power_scenario
	gen ban_mining_`bank' = `var'*mining_scenario 
	gen ban_nongrowth_`bank' = `var'*nongrowth_scenario 
	gen ban_lowgrowth_`bank' = `var'*lowgrowth_scenario 
	gen ban_lowshare_`bank' = `var'*lowshare_scenario 
	gen ban_highshare_`bank' = `var'*highshare_scenario 

}




gcollapse (sum) ban_*, by(year)
greshape long ban_ ban_power_ ban_mining_ ban_nongrowth_ ban_lowgrowth_ ban_lowshare_ ban_highshare_, i(year) j(BankID)
rename ban_ banned_scenario_count
gen ban_intensity = banned_scenario_count/`scenario_count'
gen ban_intensity_power = ban_power_/`scenario_count_power'
gen ban_intensity_mining = ban_mining_/`scenario_count_mining'
gen ban_intensity_nongrowth = ban_nongrowth_/`scenario_count_nongrowth'
gen ban_intensity_lowgrowth = ban_lowgrowth_/`scenario_count_lowgrowth'
gen ban_intensity_lowshare = ban_lowshare_/`scenario_count_lowshare'
gen ban_intensity_highshare = ban_highshare_/`scenario_count_highshare'



sort BankID year
order BankID year

keep BankID year ban_intensity ban_intensity_power ban_intensity_mining ban_intensity_nongrowth ban_intensity_lowgrowth ban_intensity_lowshare ban_intensity_highshare ban_lowshare_ ban_highshare_
ren ban_intensity ban_intensity_new1
compress
save ../Intermediate/policy_intensity_yearly_new, replace


********************************************************************************
*  POLICY CODING WITHOUT UPDATING DIVESTMENT POLICY
********************************************************************************
use "../Intermediate/policy_coding_setup_blank", clear
include "../Data_Cleaning/Policy_Coding_Function_Min.do"
//get rid of extra secenarios used for graph
drop if scenario > 100

count if year == 2009
local scenario_count = r(N)
gcollapse (sum) ban_*, by(year)
greshape long ban_ , i(year) j(BankID)
rename ban_ banned_scenario_count
gen ban_intensity_initial = banned_scenario_count/`scenario_count'
keep BankID year ban_intensity_initial
tempfile non_updating
save `non_updating'

use ../Intermediate/policy_intensity_yearly_new, clear
merge 1:1 BankID year using `non_updating', nogen 
compress
save ../Intermediate/policy_intensity_yearly_new, replace

